<template>

	<view >
		<!-- <u--input placeholder="请输入关键词" border="bottom" @change="filterKey"></u--input> -->

		<view style="padding: 0 32rpx;">
				
			<u-list @scrolltolower="scrolltolower">
				<u-checkbox-group v-model="checkboxValue" placement="column" @change="checkBoxChange">
					<u-row>
						<u-col span="2"    >
							<u--text text="名称"></u--text>
						</u-col>
						<u-col span="4">

							<u--text text="过期时间"></u--text>
						</u-col>
						<u-col span="3">
							<u--text text="所属店铺"></u--text>
						</u-col>
						<u-col span="3">
							<u--text text="临期配置"></u--text>
						</u-col>
					</u-row>
					<u-empty
						 
					        mode="data"
							iconSize='120'
							textSize='18'
							marginTop='150'
					       v-if="goodsList.length <=0"
					>
					</u-empty> 
						<u-list-item v-else v-for="(item, index) in goodsList" :key="index" @click='select'>
							<u-row customStyle="margin-top: 16rpx;"   >
								<u-col span="2"  >
									<u--text :text="item.goods_name || '暂无'"></u--text>
								</u-col>
						
								<u-col span="4">
									<u-text v-if="!item.expiration_date"  text=" 暂无 "></u-text>
									<u-text v-else mode="date" :text="Number(item.expiration_date)"></u-text>
								</u-col>
								<u-col span="3">
									<u--text :text="shopName"></u--text>
								</u-col>
								<u-col span="2">
									<view class="checkBox" @click="temporary(item,index)">
										<view class="boxIcon" v-if="item.temporary==1">
											<span class="iconfont icon-duihao" style='font-weight: bold;
											font-size: 50rpx;
											color: #3c9cff;'></span>

										</view>
									</view>
									<!-- <u-checkbox
									:name='item.id'
								 
									></u-checkbox> -->
									 <!-- <u-button type="primary" :text="item.temporary==1?'取消':'选择'" @click="temporary(item,index)" size="small"></u-button> -->
								</u-col>
							</u-row>
						</u-list-item>
				 
					
				</u-checkbox-group>

			</u-list>
			<u--text color='#7a7a7a' text='提醒'></u--text>
			<u--text color='#7a7a7a' text='1.向左侧滑动,可删除或修改配置,只有VIP店主才有移除权限;'></u--text>
			<u--text color='#7a7a7a' text='2.若删除货架名称,货架上的商品不会丢失;'></u--text>
			<u--text color='#7a7a7a' text='3.若修改货架名称,已录入商品货的货架号信息都会被修改为新货架名称;'></u--text>
			<view class="" style="position: fixed;bottom: 20rpx;width: 700rpx;">
				<u-row>
			
					<u-col span="12">
					 
					</u-col>
				</u-row>
			</view>
		</view>


		
		<view style="  position: fixed;bottom: 35rpx;width: 750rpx;background-color: #fff;">
			<u-row>
				<u-col span="8" offset="2"> 
					<u-button type="primary" shape='circle' @click="reback" text="确定"></u-button>
				</u-col>
			</u-row>
		</view>
	</view>
</template>

<script>
	var that
	var ERgoodsShelf = require('../../../../service/ERgoodsShelf.js'), mERgoodsShelf;
	var ERGoodsScan = require('../../../../service/ERGoodsScan.js'), mERGoodsScan;
	export default {
		data() {
			return {
				checkboxValue: [],
				shelfs: [],
				goods: [],
				userInfo: {},
				fillterList: [],
				shopName:'',
				value1: 1,
				goodsList: [],
				shopId:''
			}
		},
		onLoad(op) {
			that = this
			if(op.shopId){
				that.shopId = op.shopId
			}
			mERgoodsShelf = new ERgoodsShelf(this.mNetTool)
			mERGoodsScan = new ERGoodsScan(this.mNetTool)
		},
		mounted() {
			that.userInfo = uni.getStorageSync('userInfo')
			that.getShelfList()
			
			let viewShopListItem = uni.getStorageSync('viewShopListItem')
			let a = []
			if(!viewShopListItem){
				uni.$u.toast('请先选择店铺')
				return
			}else{
				that.shopName = viewShopListItem.shop_name
			}
			// shelfList.forEach( item => {
			// 	a.push(item.id)
			// })
			console.log(a)
			uni.showLoading({
				title:'获取中'
			})
			mERGoodsScan.list({
				own_store_id:viewShopListItem.id
			},res => {
				console.log(res)
				uni.hideLoading()
				that.goodsList = res.rows
				that.initCheckBox()
				
			}) 
		},
		methods: {
			initCheckBox(){
				let oarr = that.goodsList
				let a = []
				oarr.forEach(item => {
					if(item.temporary == 1){
						a.push(item.id)
					}
				})
				that.checkboxValue = a
			},
			reback(){
				let oarr = that.goodsList
				let checked = []
				let unChecked = []
				oarr.forEach(item => {
					if(item.temporary == 1){
						checked.push(item.id)
					}else{
						unChecked.push(item.id)
					}
				})
				uni.showLoading({
					title:'请稍等'
				})
				console.log('选中的',checked)
				console.log(unChecked)
				return
				
				mERGoodsScan.update({
					id:checked.toString(),
					temporary:1,
					change_person:that.userInfo.nick_name,
					updated_by:that.userInfo.id,
				},res => {
					console.log(res)
					mERGoodsScan.update({
						id:unChecked.toString(),
						temporary:0,
						change_person:that.userInfo.nick_name,
						updated_by:that.userInfo.id,
					},res => { 
						uni.hideLoading()
						uni.navigateBack({
							
						})
					})
					 
				})
			},
			temporary(item,index){
				console.log(index)
				let oarr = that.goodsList 
				if(item.temporary === 1){
					oarr[index].temporary = 0
					that.goodsList = oarr
					mERGoodsScan.update({
						id:item.id,
						temporary:0,
						change_person:that.userInfo.nick_name,
						updated_by:that.userInfo.id,
					},res => {
						// that.goodsList[index].temporary = 0
						uni.$u.toast('移除成功')
						 
					})
				}else{
					oarr[index].temporary = 1
					that.goodsList = oarr
					mERGoodsScan.update({
						id:item.id,
						temporary:1,
						change_person:that.userInfo.nick_name,
						updated_by:that.userInfo.id,
					},res => {
						that.goodsList[index].temporary = 1
						uni.$u.toast('添加成功')
						 
					})
				}
				
			}, 
			checkBoxChange(e) {
				console.log(e)
				if(!e){
					return
				} 
				let arr = e
				let oarr = that.goodsList
				for(let i = 0;i<oarr.length;i++){
					for(let j=0;j<arr.length;j++){
						if(oarr[i].id == arr[j]){
							oarr[i].temporary = 1
							continue
						}else{
							oarr[i].temporary = 0
						}
					}
				} 
				// 重新归类
				that.goodsList = oarr
				// temporary
			},
			add() {
				uni.showToast({
					title: '请稍等'
				})
				console.log(that.checkboxValue.toString())
				let ids = {
					id: that.checkboxValue.toString(),
					temporary: 1
				}
				mERGoodsScan.update(ids, res => {
					console.log(res)
					uni.showToast({
						title: '新增成功'
					})
					uni.hideToast()
				})
			},
			getShelfList() {


			},
			open(e) {
				// console.log('open', e)
			},
			close(e) {
				// console.log('close', e)
			},
			change(e) {
				// console.log('change', e)
			},
			filterKey(e) {
				console.log(e)
				that.fillterList = that.goods.filter(item => {
					return item.goods_name.indexOf(e) != -1
				})
				console.log(a)
			},
		}
	}
</script>

<style lang="scss">
	@import '/static/iconfont/iconfont.css' ;
	.checkBox{
		width: 60rpx;
		height: 60rpx;
		border: 2rpx solid #3c9cff;
		border-radius: 10rpx;
		text-align: center;
		line-height: 60rpx;
	}
</style>
